/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// DataInt.cpp: implementation of the CDataInt class.
//
//////////////////////////////////////////////////////////////////////

#include <ctype.h>
#include <math.h>
#include "ssi_dataint.h"

#include "ssi_datareal.h"
#include "ssi_databool.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SSI_SCRIPTDLL_API SSI_INT64 CDataInt::sType = 0;


CDataInt::CDataInt()
{
  Data = 0;
	if(!sType){
		sType = 1;
		sType = Factory_RegisterData((CData*)new CDataInt());
	}

	Type = sType;
	DataName = "int";
}

CDataInt::~CDataInt()
{

}

SSI_BOOL CDataInt::boolean()
{
  if(Data) return 1;
  return 0;
}

String CDataInt::GetString()
{
  String Ret;
  Ret.Format("%d", Data);
  return Ret;
}

SSI_BOOL CDataInt::DataFromString(const String& sData)
{
  SSI_INT64 ctr = 0;
	SSI_CHAR c;
	String s = sData.Str();
  if(sData == "")
  {
    Data = 0;
    return 0;
  }
	if(s.Find(".") >= 0){
		return 0;
	}
  //if((NumFields = sscanf(sData.Str(), "%d", &Data)) == 1) return 1;

	for(ctr = 0; ctr < sData.GetLength(); ctr++){
		c = sData[ctr];
		if(!isdigit(c)){
			if((c != '-') && (c != '+')){
				return 0;
			}
		}
	}

	sscanf(sData.Str(), "%d", &Data);
  return 1;

}

SSI_BOOL CDataInt::CopyData(CData* Other)
{
  if(Type != Other->Type) return 0;
  if(Other == SSI_NULL) return 0;
  CDataInt* Temp = (CDataInt*)Other;
  Data = Temp->Data;
  return 1;
}

SSI_INT64 CDataInt::GetData()
{
  return Data;
}

SSI_INT64 CDataInt::SizeOf()
{
  return sizeof(Data);
}

SSI_INT64 CDataInt::GetRegisteredType(void)
{
	if(!sType){
		CDataInt aData;
	}
	return sType;
}

CData* CDataInt::operator_plus(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataReal::GetRegisteredType()){
    Data += (SSI_INT64)((CDataReal*)InData)->Data;
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data += ((CDataInt*)InData)->Data;
  }

//	if(InData->GetType() != Type) return SSI_NULL;
//	Data+=((CDataInt*)InData)->Data;
	return (CData*)this;
}

CData* CDataInt::operator_minus(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataReal::GetRegisteredType()){
    Data -= (SSI_INT64)((CDataReal*)InData)->Data;
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data -= ((CDataInt*)InData)->Data;
  }
	return (CData*)this;
}

CData* CDataInt::operator_bit_and(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data & ((CDataInt*)InData)->Data;
  }
	return (CData*)this;
}

CData* CDataInt::operator_bit_or(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data | ((CDataInt*)InData)->Data;
  }
	return (CData*)this;
}


CData* CDataInt::operator_multiply(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataReal::GetRegisteredType()){
    Data = (SSI_INT64)((REAL)Data * ((CDataReal*)InData)->Data);
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data * ((CDataInt*)InData)->Data;
  }

	return (CData*)this;
}

CData* CDataInt::operator_modulous(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data % ((CDataInt*)InData)->Data;
  }
	return (CData*)this;
}

CData* CDataInt::operator_divide(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataReal::GetRegisteredType()){
    Data = (SSI_INT64)((REAL)Data / ((CDataReal*)InData)->Data);
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data / ((CDataInt*)InData)->Data;
  }

	//if(InData->GetType() != Type) return SSI_NULL;
	//Data = Data / ((CDataInt*)InData)->Data;
	return (CData*)this;
}

CData* CDataInt::operator_power(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataReal::GetRegisteredType()){
    Data = (SSI_INT64)pow((REAL)Data, ((CDataReal*)InData)->Data);
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = pow(Data, ((CDataInt*)InData)->Data);
  }
	
	//if(InData->GetType() != Type) return SSI_NULL;
	//Data = pow(Data, 
	//Data = Data ^ ((CDataInt*)InData)->Data;
	return (CData*)this;
}

CData* CDataInt::operator_equal(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataReal::GetRegisteredType()){
    Data = ((CDataReal*)InData)->Data;
  }else if(InData->GetType() == CDataBool::GetRegisteredType()){
    if(((CDataBool*)InData)->Data){
      Data = 1;
    }else{
      Data = 0;
    }
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = ((CDataInt*)InData)->Data;
  }
	return (CData*)this;
}

CStatus CDataInt::negate(void)
{
	Data = -1 * Data;
	return STAT_OK;
}

CStatus CDataInt::operator_compare(CData* InData, SSI_INT64& Result)
{
  if(InData->GetType() != Type && InData->GetType() != CDataReal::GetRegisteredType()){
    return CData::operator_compare(InData, Result);
  }

  if(InData->GetType() == Type){
	CDataInt* In = (CDataInt*)InData;
	if(Data < In->Data){
		Result = -1;
	}else if(Data > In->Data){
		Result = 1;
	}else{
		Result = 0;
	}
  }else if(InData->GetType() == CDataReal::GetRegisteredType()){
	  CDataReal* In = (CDataReal*)InData;
	  if(Data < In->Data){
		  Result = -1;
	  }else if(Data > In->Data){
		  Result = 1;
	  }else{
		  Result = 0;
	  }
  }
	return STAT_OK;
}

CStatus CDataInt::operator_increment()
{
	Data++;
	return STAT_OK;
}

CStatus CDataInt::operator_decrement()
{
	Data--;
	return STAT_OK;
}

SSI_INT64 CDataInt::Register(SSI_INT64 NewType){
	sType = NewType;
	return 1;
}

void CDataInt::UnRegister(){
	sType = 0;
}




